package com.yy.yycompnent.slide
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;

	/**
	 *文本滚动控制器 
	 * @author YY
	 * 
	 */
	public class TextScrollLider
	{
		private var _view:MovieClip;//资源
		private var _content:TextField;//文本内容
		private var _scrollBar:ScrollBar;//滚动条
		public function TextScrollLider(view:MovieClip)
		{
			_view = view;
			init();
		}
		
		/**
		 *初始化控制器 
		 * 
		 */
		private function init():void {
			_content = _view.content;
			_scrollBar = new ScrollBar(_view.sliderView.upBtn, _view.sliderView.downBtn, _view.sliderView, changeFunc);
			
		}
		
		private function changeFunc(e:Event):void {
			var value:int = _scrollBar.value;
			trace(value);
			_content.scrollV = value;
		}
		
		/**
		 *清空文本 
		 * 
		 */
		public function clear():void {
			_content.text = "";
			_scrollBar.setThumbPercent(1);
		}
		/**
		 *添加文本 
		 * 
		 */
		public function addContent(str:String, autoToBotton:Boolean=false):void {
			_content.appendText(str);
			_scrollBar.setThumbPercent((_content.bottomScrollV - _content.scrollV)/(_content.scrollV-_content.bottomScrollV + _content.maxScrollV));
			if(autoToBotton) {
				toBotton();
			}else {
				_scrollBar.setSliderParams(1, _content.maxScrollV,_content.scrollV);
			}
		}
		
		/**
		 *显示指定文本 
		 * @param str
		 * 
		 */
		public function showText(str:String, autoToBotton:Boolean=false):void {
			_content.text = String(str);
			if(_content.maxScrollV == _content.scrollV) {
				_scrollBar.setThumbPercent(1);
			}else {
				var showLineCount:int = _content.bottomScrollV - _content.scrollV + 1; 
				var temp:Number = showLineCount/(showLineCount+_content.maxScrollV);
				_scrollBar.setThumbPercent(temp);
			}
			
			if(autoToBotton) {
				toBotton();
			}else {
				_scrollBar.setSliderParams(1, _content.maxScrollV,_content.scrollV);
			}
		}
		
		
		/**
		 *到顶部 
		 * 
		 */
		public function toTop():void {
			_scrollBar.setSliderParams(1, _content.maxScrollV,1);
		}
		
		/**
		 *移动到底部 
		 * 
		 */
		public function toBotton():void {
			_scrollBar.setSliderParams(1, _content.maxScrollV,_content.maxScrollV);
		}
	}
}